package pl.p10.Tool;

import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;

import org.antlr.runtime.tree.CommonTree;

public class ASTtoTreeModelAdapter implements TreeModel {
  private CommonTree tree = null;

  public ASTtoTreeModelAdapter(CommonTree tree) {
    this.tree = tree;
  }//Constructor

  public Object getChild(Object parent, int index) {
    return ((CommonTree) parent).getChild(index);
  }//getChild

  public int getChildCount(Object parent) {
    return ((CommonTree) parent).getChildCount();
  }//getChildCount

  public int getIndexOfChild(Object parent, Object child) {
    if (parent == null || child == null)
      return -1;
    CommonTree cp = ((CommonTree) parent);
    CommonTree cc = ((CommonTree) child);
    for (int i = 0; i < cp.getChildCount(); i++) {
      if (cp.getChild(i).equals(cc))
        return i;
    }//for
    return -1;
  }//getIndexOfChild

  public Object getRoot() {
    return tree;
  }//getRoot

  /*
   * (non-Javadoc)
   * @see javax.swing.tree.TreeModel#isLeaf(java.lang.Object)
   */
  public boolean isLeaf(Object node) {
    CommonTree cn = ((CommonTree) node);
    boolean r = (cn.getChildCount() == 0) ? true : false;
    return r;
  }//isLeaf

  public void addTreeModelListener(TreeModelListener l) {
    // TODO Auto-generated method stub
  }//addTreeModelListener

  public void removeTreeModelListener(TreeModelListener l) {
    // TODO Auto-generated method stub
  }//removeTreeModelListener

  public void valueForPathChanged(TreePath path, Object newValue) {
    // TODO Auto-generated method stub
  }//valueForPathChanged

}//ASTtoTreeModelAdapter
